/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.ec.tesis.prototiporecomendador.ui;

import edu.ec.tesis.prototiporecomendador.dao.GenericJpaController;
import edu.ec.tesis.prototiporecomendador.mdl.Loging;
import edu.ec.tesis.prototiporecomendador.mdl.User;
import java.io.Serializable;
import javax.annotation.Resource;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.ViewScoped;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.transaction.UserTransaction;

/**
 *
 * @author Johnny
 */
@ManagedBean(name = "ctrRegistro")
@ViewScoped
public class CtrRegistro implements Serializable{

    /**
     * Creates a new instance of CtrRegistro
     */
    @Resource
    private UserTransaction utx = null;
    @PersistenceUnit(unitName = "recomendadorPU")
    private EntityManagerFactory emf = null;
    
    GenericJpaController<User> controladorUsuario;
    GenericJpaController<Loging> controladorLogin;
    
    private GenericJpaController<User> getJpaUserController()
    {
        if(controladorUsuario==null)
            controladorUsuario= new GenericJpaController<User>(utx, emf);
        return controladorUsuario;
    }
    
    private GenericJpaController<Loging> getJpaLoginController()
    {
        if(controladorLogin==null)
            controladorLogin= new GenericJpaController<Loging>(utx, emf);
        return controladorLogin;
    }
    private Loging user;
    
    
    public CtrRegistro() {
        user= new Loging();
        user.setIdUser(new User());
    }

    public Loging getUser() {
        return user;
    }

    public void setUser(Loging user) {
        this.user = user;
    }
    
    public String guardar()
    {
       try{
           user.getIdUser().setIdUser(getJpaUserController().findEntities(1, getJpaUserController().getCount(new User())-1, new User()).get(0).getIdUser()+1);
           getJpaUserController().create(user.getIdUser());
           
           getJpaLoginController().create(user);
           return "index.xhtml?faces-redirect=true";
       }
       catch(Exception ex)
       {
           ex.printStackTrace();
           return null;
       }
        
    }
    
}
